package controllers

import (
	"durg/models"
	"encoding/json"
	beego "github.com/beego/beego/v2/server/web"
	"strconv"
)

type MedicalController struct {
	beego.Controller
}

// 添加
func (c *MedicalController) Add() {
	name := c.GetString("name")
	sex := c.GetString("sex")
	tel := c.GetString("tel")
	doctor := c.GetString("doctor")
	time := c.GetString("time")
	num, _ := c.GetInt("num")
	status := c.GetString("status")
	explain := c.GetString("explain")
	medical := models.Medical{
		Name:    name,
		Sex:     sex,
		Tel:     tel,
		Doctor:  doctor,
		Time:    time,
		Num:     num,
		Status:  status,
		Explain: explain,
	}
	err := models.DB.Model(&models.Medical{}).Create(&medical).Error
	if name == "" || sex == "" || tel == "" || doctor == "" || time == "" || status == "" || explain == "" {
		c.Data["json"] = JsonR{
			Code: 500,
			Msg:  "各个字段不能为空",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	if num <= 0 {
		c.Data["json"] = JsonR{
			Code: 500,
			Msg:  "各个字段不能为空",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 500,
			Msg:  "添加失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "添加成功",
			Data: medical,
		}
		c.ServeJSON()
	}
}

// 列表
func (c *MedicalController) List() {
	page, _ := c.GetInt("page", 1)
	size, _ := c.GetInt("size", 10)
	pageData := make(map[string]interface{})
	query := models.DB.Model(&models.Medical{})
	name := c.GetString("name")
	if name != "" {
		query = query.Where("name like ?", "%"+name+"%")
	}
	key := "medical-" + strconv.Itoa(page) + "-" + name
	val, err := models.Rdb.Get(key).Result()
	if err != nil {
		var count int64
		query.Count(&count)
		var medical []models.Medical
		offset := (page - 1) * size
		err = query.Offset(offset).Limit(size).Find(&medical).Error
		if err != nil {
			c.Data["json"] = JsonR{
				Code: 500,
				Msg:  "查询失败",
				Data: nil,
			}
			c.ServeJSON()
			return
		} else {
			pageData["count"] = count
			pageData["page"] = page
			pageData["medical"] = medical
			marshal, _ := json.Marshal(&pageData)
			value := string(marshal)
			models.Rdb.Set(key, value, 0)
			c.Data["json"] = JsonR{
				Code: 200,
				Msg:  "查询成功-mysql",
				Data: pageData,
			}
			c.ServeJSON()
		}
	} else {
		json.Unmarshal([]byte(val), &pageData)
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "查询成功-redis",
			Data: pageData,
		}
		c.ServeJSON()
	}
}

// 详情
func (c *MedicalController) Info() {
	id, _ := c.GetInt("id")
	var medical models.Medical
	key := "medical-" + strconv.Itoa(id)
	RankKey := "medical-rank"
	val, err := models.Rdb.Get(key).Result()
	if err != nil {
		err = models.DB.Model(&models.Medical{}).Where("ID=?", id).First(&medical).Error
		if err != nil {
			c.Data["json"] = JsonR{
				Code: 500,
				Msg:  "详情展示失败",
				Data: nil,
			}
			c.ServeJSON()
			return
		} else {
			marshal, _ := json.Marshal(&medical)
			value := string(marshal)
			models.Rdb.Set(key, value, 0)
			models.Rdb.ZIncrBy(RankKey, 1, medical.Name)
			c.Data["json"] = JsonR{
				Code: 200,
				Msg:  "详情展示成功-mysql",
				Data: medical,
			}
			c.ServeJSON()
		}
	} else {
		json.Unmarshal([]byte(val), &medical)
		models.Rdb.ZIncrBy(RankKey, 1, medical.Name)
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "详情展示成功-redis",
			Data: medical,
		}
		c.ServeJSON()
	}
}

// 修改状态
func (c *MedicalController) Up() {
	id, _ := c.GetInt("id")
	status := c.GetString("status")
	medical := models.Medical{
		Status: status,
	}
	err := models.DB.Model(&models.Medical{}).Where("ID=?", id).Updates(&medical).Error
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 500,
			Msg:  "修改状态失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "修改状态成功",
			Data: medical,
		}
		c.ServeJSON()
	}
}

// 排行榜
func (c *MedicalController) Rank() {
	key := "medical-rank"
	nums := models.Rdb.ZCard(key).Val()
	ranks, err := models.Rdb.ZRangeWithScores(key, 0, nums-1).Result()
	if err != nil {
		c.Data["json"] = JsonR{
			Code: 500,
			Msg:  "排行榜失败",
			Data: nil,
		}
		c.ServeJSON()
		return
	} else {
		c.Data["json"] = JsonR{
			Code: 200,
			Msg:  "排行榜成功",
			Data: ranks,
		}
		c.ServeJSON()
	}
}
